简体   繁体   English

用jquery click事件触发AJAX

[英]Trigger AJAX with jquery click event

I have two drop down menus, one of which I am trying to replace with radio buttons using jquery. 我有两个下拉菜单,我正在尝试使用jquery将其中一个替换为单选按钮。 The second box is updated via AJAX with new options any time the user makes a selection in the first drop down. 每当用户在第一个下拉列表中进行选择时,第二个框就会通过AJAX使用新选项进行更新。 I have successfully generated radio buttons that change the values for the first drop down but when the user updates the first drop down menu using the radio buttons, it no longer effects values in the second drop down boxes. 我已经成功生成了单选按钮,可以更改第一个下拉菜单的值,但是当用户使用单选按钮更新第一个下拉菜单时,它不再影响第二个下拉框中的值。 I am not great with AJAX or JS and I can't figure out how to trigger the AJAX load when the user selects one of radio buttons. 我对AJAX或JS不太满意,并且当用户选择单选按钮之一时,我不知道如何触发AJAX加载。

I apologize in advance for the wall of code, Im not sure what is important so I included everything that seemed relevant. 我预先为代码墙道歉,我不确定什么是重要的,所以我列出了所有似乎相关的内容。 If you want to see the page in question you can see it here. 如果要查看有问题的页面,可以在这里查看。

The code I am using to generate radio buttons looks like this: 我用来生成单选按钮的代码如下所示:

 $(function(){
    $("#options-1 option").each(function(i, e) {
        $("<input type='radio' name='r' />")
        .attr("value", $(this).val())
        .attr("checked", i == 0)
        .click(function () {
            $("#options-1").val($(this).val());
        })
        .appendTo("#r");
       $("#options-1").change(function(){
       $("input[name='r'][value='"+this.value+"']").attr("checked","checked");
});
});
});

$("#options-1").change(function(){
    $("input[name='r'][value='"+this.value+"']").attr("checked","checked");
});

The HTML for the drop downs look like this: 下拉菜单的HTML如下所示:

<form action="http://example.com/dev3/?page_id=5" method="post" class="shopp product validate">
<div id="r"></div>

<ul class="variations">
<li> 
<label for="options-1">Framing</label> 
<select name="products[1][options][]" class="category-catalog product1 options" id="options-1"><option value="1">Print Only (Unframed)</option> 
<option value="2">Professionally Framed</option> 
</select><li> 
<label for="options-2">Size</label> 
<select name="products[1][options][]" class="category-catalog product1 options" id="options-2"><option value="3">12 x 8</option> 
<option value="4">24 x 36</option> 
</select></li> 

</ul>

<p><input type="hidden" name="products[1][product]" value="1" /><input type="hidden" name="products[1][category]" value="catalog" /><input type="hidden" name="cart" value="add" /><input type="submit" name="addtocart"  value="Add to Cart" class="addtocart" /></p>

</form>

The AJAX looks like this: AJAX看起来像这样:

<script type='text/javascript' src='http://example.com/dev3?sjsl=colorbox,shopp,catalog,cart&amp;c=1&amp;ver=98239bb061a58639408323699680ad0e'></script> 
<script type='text/javascript'> 
/* <![CDATA[ */
var ShoppSettings = {
    ajaxurl: "http://example.com/dev3/wp-admin/admin-ajax.php",
    cp: "1",
    c: "$",
    p: "2",
    t: " ",
    d: ".",
    g: "3",
    nocache: "",
    opdef: ""
};
    var pricetags = {};
jQuery(window).ready(function(){ var $ = jqnc(); 
        pricetags[1] = {};
    pricetags[1]['pricing'] = {"18770":{"p":10,"i":false,"s":false,"t":"Shipped"},"25785":{"p":21,"i":true,"s":"1","t":"Shipped"},"23510":{"p":20,"i":false,"s":false,"t":"Shipped"}};
    pricetags[1]['menu'] = new ProductOptionsMenus('select.category-catalog.product1.options',true,pricetags[1]['pricing'],0);

});
/* ]]> */
</script> 

When you change the value of a Combo Box via js, it doesn't trigger the onChange function nor onClick and so on. 通过js更改组合框的值时,它不会触发onChange函数或onClick等。

You'll have to use the same code you use to update Combo 2 with Combo 1 where you update Combo 1 with the Radio. 您将必须使用与使用Combo 1更新Combo 2的代码相同,而在其中使用Radio来更新Combo 1。

I suggest to place that code inside another function, and call that function from both places. 我建议将该代码放在另一个函数中,然后从两个地方调用该函数。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM