2016-06-14 51 views
0

我有2個表格。當我提交frm1時,我有一些檢查選項並使用變量checkedValues來存儲它。循環並提交表格jquery

我在每個選中的選項中循環,將值更改爲frm2,提交併在3秒後打開彈出窗口。但它不適用於提交表單2和setTimeout函數。

有沒有人有任何想法來解決這個問題?

<form action="" method="post" name='frm1'> 
    <input type="checkbox" name="1" id="1" value="1" class="checkbox"> 
    <input type="checkbox" name="2" id="2" value="1" class="checkbox"> 
    <input type="checkbox" name="3" id="3" value="0" class="checkbox"> 
    <input type="checkbox" name="4" id="4" value="0" class="checkbox"> 
    <input type="submit" value='add'> 
</form> 

<form action='http://example.com' method='post' name='frm2' target="_blank"> 
    <input type="text" name="fname" class="form-control" value=''> 
    <input type="text" name="fvalue"class="form-control" value=''> 
</form> 

<script> 
$('#add').click(function(){ 
    var store= <?php echo json_encode($store)?>; 
    var checkedValues = $('input:checkbox:checked').map(function() { 
      return this.id; 
     }).get(); 

    $.each(checkedValues, function(index,val) { 

     document.frm2.fname.value = store[val]['name']; 
     document.frm2.fvalue.value = store[val]['value']; 

     document.frm2.submit(); // not working 

     setTimeout(function() {  // not working 
      window.open("http://example.com/client, "_blank"), 3000); 
     }); 
    }); 
}); 
</script> 
+0

表單沒有值。輸入具有值。你想在這裏做什麼? 'document.frm2.value.value = store [val] ['value'];' –

+0

此外,爲什麼不使用'console.log()'來查看'$'的前2個賦值.each()'迭代?檢查目標(以驗證它們是您打算定位的目標)並分配新值。 –

+0

您的表單一提交,您就會失去控制權。在提交表單之前打開彈出窗口。 – dave

回答

0

這裏的問題是,您的第一個表單正在被提交,所以您的頁面被重新加載並且腳本的第二部分未被執行。爲了防止你應該使用AJAX。

<input type="checkbox" name="1" id="1" value="1" class="checkbox"> 
<input type="checkbox" name="2" id="2" value="1" class="checkbox"> 
<input type="checkbox" name="3" id="3" value="0" class="checkbox"> 
<input type="checkbox" name="4" id="4" value="0" class="checkbox"> 
<button id="bt-add">add</button> 

<form action='http://example.com' method='post' id="second-form" name='frm2' target="_blank"> 
    <input type="text" name="name" class="form-control" value=''> 
    <input type="text" name="value" class="form-control" value=''> 
</form> 

<script> 
$('#bt-add').click(function(){ 
    var store= <?php echo json_encode($store)?>; 
    var checkedValues = $('input:checkbox:checked').map(function() { 
     return this.id; 
    }).get(); 

    $.each(checkedValues, function(index,val) { 
     $("#second-form").find("input[name='name']").val(store[val]['name']); 
     $("#second-form").find("input[name='value']").val(store[val]['value']); 

     $.ajax({ 
      url: "http://example.com", 
      type:'GET', 
      data: $("#second-form").serialize() 
     }); 

    }); 

    setTimeout(function() { 
     window.open("http://example.com/client", "_blank"); 
    }, 3000); 
}); 

</script> 

小提琴here

+0

謝謝你回答我。我嘗試你的方式,但它不工作。兩個輸入字段更新值,但表單不會發送。如果我使用window.open(「http://example.com/client」,「_blank」);沒有settimeout,它也會彈出。這意味着腳本從上到下顯然執行,但由於某種原因,它不像我預期的那樣工作。 – Thuclb

+0

@Thuclb發生了什麼事? –

+0

@Thuclb我更新了代碼。看看它現在是否正在工作。 –