2015-12-23 14 views
2

我只在3個輸入(2個輸入,1個選擇下拉菜單)被設置爲特定值時纔會調用一個函數。該表單包含多個輸入,請選中&複選框。此調用在表單發送之前執行。jQuery僅當2個輸入和1個選擇被設置或更新時如何調用函數?

  • 第一個輸入是選擇/下拉菜單。

  • 第二個是標準輸入,只有當它至少有1個數字(最多沒有限制),一個點和一個數字時才應該檢查。示例:19212.6

  • 第三個是一個隱藏的輸入,其值應大於1。

我知道jQuery中的.change功能要幫我,但我不知道如何調整它正確地爲我的設置。最重要的是,如果輸入與上面提到的過濾器相匹配,它應該在之後運行MsSQL Query(PHP)。

$('input').change(function(){ 
} 

該代碼與PHP混合使用。
目前,我創建了3個更改(每個輸入/選擇1個),調用一個函數並檢查所有值是否符合要求。 它是一個更好的,不錯的&乾淨的方式來做到這一點?代碼

部分:

<SCRIPT language="javascript"> 
$(document).ready(function() 
    $(".gdctype").change(function() { 
     var gdc = $(this).find('option:selected').text(); 
     if (gdc == "UK"){ 
      $('.ilsel').css({ 'display': 'none'}); 
      $('.casel').css({ 'display': 'none'}); 
      $('.bslsel').css({ 'display': 'none'}); 
      $('.rusel').css({ 'display': 'none'}); 
      $('.uspsel').css({ 'display': 'none'}); 
      $('.uksel').removeAttr('style'); 
     } 
     if (gdc == "IL"){ 
      $('.uksel').css({ 'display': 'none'}); 
      $('.casel').css({ 'display': 'none'}); 
      $('.bslsel').css({ 'display': 'none'}); 
      $('.rusel').css({ 'display': 'none'}); 
      $('.uspsel').css({ 'display': 'none'}); 
      $('.ilsel').removeAttr('style'); 
     } 
     if (gdc == "CA"){ 
      $('.uksel').css({ 'display': 'none'}); 
      $('.ilsel').css({ 'display': 'none'}); 
      $('.bslsel').css({ 'display': 'none'}); 
      $('.rusel').css({ 'display': 'none'}); 
      $('.uspsel').css({ 'display': 'none'}); 
      $('.casel').removeAttr('style'); 
     } 
     if (gdc == "BSL"){ 
      $('.uksel').css({ 'display': 'none'}); 
      $('.ilsel').css({ 'display': 'none'}); 
      $('.casel').css({ 'display': 'none'}); 
      $('.rusel').css({ 'display': 'none'}); 
      $('.uspsel').css({ 'display': 'none'}); 
      $('.bslsel').removeAttr('style'); 
     } 
     if (gdc == "RU"){ 
      $('.uksel').css({ 'display': 'none'}); 
      $('.ilsel').css({ 'display': 'none'}); 
      $('.casel').css({ 'display': 'none'}); 
      $('.bslsel').css({ 'display': 'none'}); 
      $('.uspsel').css({ 'display': 'none'}); 
      $('.rusel').removeAttr('style'); 
     } 
     if (gdc == "US"){ 
      $('.uksel').css({ 'display': 'none'}); 
      $('.ilsel').css({ 'display': 'none'}); 
      $('.casel').css({ 'display': 'none'}); 
      $('.bslsel').css({ 'display': 'none'}); 
      $('.rusel').css({ 'display': 'none'}); 
      $('.uspsel').removeAttr('style'); 
     } 
    }); 

     var uniquePUS = <?php if($uniquePUS!=NULL) echo $uniquePUS;?> <?php if($uniquePUS==NULL) echo "1";?>; 
     $('.addPUS').click(function() { 
     var copy = $('#PUS').clone(true,true); 
     var formId = 'PUS' + window.uniquePUS; 
     copy.attr('id', formId); 
     copy.removeAttr('style'); 

     copy.find(':input#PUSAddress').each(function() { 
      $(this).replaceWith("<input type='text' id='" + $(this).attr("id") + window.uniquePUS+"' id='" + $(this).attr("name") + window.uniquePUS+"' >"); 
     }); 

     copy.find(':input#PUSCity').each(function() { 
      $(this).replaceWith("<input type='" + $(this).attr("type") + "' id='" + $(this).attr("id") + window.uniquePUS+"' name='" + $(this).attr("name") + window.uniquePUS + "' />"); 
     }); 

     copy.find(':input#PUSState').each(function() { 
      $(this).replaceWith("<input type='" + $(this).attr("type") + "' id='" + $(this).attr("id") + window.uniquePUS+"' name='" + $(this).attr("name") + window.uniquePUS + "' size=2 />"); 
     }); 

     copy.find(':input#PUSZip').each(function() { 
      $(this).replaceWith("<input type='" + $(this).attr("type") + "' id='" + $(this).attr("id") + window.uniquePUS+"' name='" + $(this).attr("name") + window.uniquePUS + "' size=4 />"); 
     }); 
     $('#uspselin').append(copy); 
     window.uniquePUS++; 
     $('#main').find(':input#uniquePUS').each(function() { 
      $(this).replaceWith("<input type='" + $(this).attr("type") + "' id='uniquePUS' name='" + $(this).attr("name") + "' value='"+window.uniquePUS+"' />"); 
     }); 

     }); 
    $('.delPUS').click(function() { 
     $(this).closest("ul").remove(); 
    }); 
     function chktrp(nr){ 
     if(nr != null && nr != undefined){ 
      var pattern = new RegExp(/^[0-9]+\.[0-9]+$/); 
      return pattern.test(nr);  
     } 
     else{ 
      return false; 
     } 

    } 
    function chkfields(){ 
     if (
      $('select[name="gdctype"]').val() == 'US' 
      && 
      $('input[name="uniquePUS"]').val() > '0' 
      && 
      chktrp($('input[name="Trip"]').val()) 
     ) { 
      return true; 

     }else{ 
     return false;} 
    } 
    $('input[name="Trip"]').change(function(){ 
     if (chkfields()){ 
      alert('Works1'); 
     } 
    }); 
    $('input[name="uniquePUS"]').change(function(){ 
     if (chkfields()){ 
      alert('Works2'); 
     } 
    }); 
    $('select[name="gdctype"]').change(function(){ 
     if (chkfields()){ 
      alert('Works3'); 
     } 
    }); 
}); 

如果有關我可以添加HTML代碼。

+1

你可以有單獨的JavaScript文件,單獨的PHP腳本/類有運行查詢和其他東西的功能。然後在滿足特定條件時通過ajax調用適當的函數。請告訴我們你到底想知道什麼,以及你到底做了些什麼。 –

+0

向我們顯示您的代碼。你有什麼努力去完成你想要的? – SPottuit

+0

這聽起來很完美。我想在提交表單之前根據用戶選擇的內容更新表單。 我想要完成的是一種複雜的形式,根據您選擇的內容生成不同類型的文檔。 一旦選擇了字段並設置了文件編號(示例19212.6),就會運行SQL查詢並返回與查詢,名稱和另一個函數相匹配的行號,以便將更多特徵添加到窗體(div ,複選框等)。 (文件).ready(函數(){) – poypoy

回答

0

你可以得到一個表單字段的值是這樣的:

$('input[name="inputName"]').val() 

一,你知道,你可以測試你的領域:

if (
    $('input[name="inputName"]').val() != '' 
    && 
    $('input[name="inputName2"]').val() > 2 
) { 
    .... call your function 
} 
+0

)看起來不錯,但只能使用一次表單被髮送並且頁面被重新加載。在表單被髮送之前以任何方式進行活動? – poypoy

相關問題