简体   繁体   English

如何使用jQuery / JavaScript检测多组单选按钮更改并更改单独的表单输入?

[英]How do I use jQuery/JavaScript to detect multiple sets of radio button changes and change a separate form input?

The situation is that I have nine unique values for PayPal "add to cart" that I'm trying to place into a single HTML input tag , determined by the combination of choices from two sets of radio buttons (three radio buttons in each set). 情况是,我有九个PayPal“添加到购物车”的唯一值,我正试图将其放入单个HTML input tag ,由两组radio buttons (每组中三个单选按钮)的组合决定。

The HTML could be slightly different, and the only reason it looks how it is now, is because I've been tinkering with various scripts trying to find a working method. HTML可能略有不同,它看起来现在的唯一原因是,因为我一直在修补各种脚本试图找到一种工作方法。 The scripting could probably be much simpler, but I'd just like some help with understanding how to make it function (regardless of which radio buttons are changed, or how many times). 脚本可能要简单得多,但我只想帮助理解如何使其起作用(无论哪个单选按钮被更改,或多少次)。

<form id="prod_size">
<input type="radio" name="size" value="1">small</input>
<input type="radio" name="size" value="4" checked="checked">medium</input>
<input type="radio" name="size" value="16">large</input>
</form>
<form id="prod_bundle">
<input type="radio" name="prod_bundle" value="single">single</input>
<input type="radio" name="prod_bundle" value="double">double</input>
<input type="radio" name="prod_bundle" value="triple" checked="checked">triple</input>
</form>
<form>
<input id="cart_button_value" value="green">
</form>

I don't see any errors in the browser console. 我没有在浏览器控制台中看到任何错误。 Everything below is frankensteined from multiple code examples on stackoverflow/stackexchange, because I couldn't find a working solution by itself, unfortunately. 下面的所有内容都是通过stackoverflow / stackexchange上的多个代码示例进行的,因为我遗憾地找不到一个可行的解决方案。

$("#prod_size , #prod_bundle").on('change', function() {
if ('#prod_size' == '1'){
    if ("#prod_bundle".value == 'single'){
        $("#cart_button_value").val = 'red';
    }
    else if ("#prod_bundle".value == 'double'){
        $("#cart_button_value").val = 'green';
    }
    else if ("#prod_bundle".value == 'triple'){
        $("#cart_button_value").val = 'blue';
    }
}
else if ('#prod_size' == '4'){
    if ("#prod_bundle".value == 'single'){
        $("#cart_button_value").val = 'black';
    }
    else if ("#prod_bundle".value == 'double'){
        $("#cart_button_value").val = 'white';
    }
    else if ("#prod_bundle".value == 'triple'){
        $("#cart_button_value").val = 'gray';
    }
}
else if ('#prod_size' == '16'){
    if ("#prod_bundle".value == 'single'){
        $("#cart_button_value").val = 'orange';
    }
    else if ("#prod_bundle".value == 'double'){
        $("#cart_button_value").val = 'purple';
    }
    else if ("#prod_bundle".value == 'triple'){
        $("#cart_button_value").val = 'pink';
    }
}
}).trigger('change');

Your train of thought is somewhat on the right path, but there are number of errors in your code. 您的思路在某种程度上正确,但您的代码中存在许多错误。 For instance, "#prod_bundle".value is not a valid statement. 例如, "#prod_bundle".value不是有效语句。 The only reason why you are not seeing errors is because your first level if statements inside the "onChange" function will never evaluate to true, and thus the code inside them never runs. 您没有看到错误的唯一原因是因为“onChange”函数中的第一级if语句永远不会计算为true,因此它们内部的代码永远不会运行。 This is so because you are comparing two literal strings, and string:'#prod_size' == string:'1' can never equal one another. 这是因为你正在比较两个文字字符串和string:'#prod_size' == string:'1'永远不能相等。

At any rate, here's a way to go about it: 无论如何,这是一种方法:

 // comments are in reference to the code OP posted // there is no need to fetch dom elements every time you need them // store them in variables when the need to use them arises var $prodSize = $('#prod_size') var $prodBundle = $('#prod_bundle') var $cartButton = $("#cart_button_value") // creating a separate function to handle the change // is not strictly necessary, but it makes things a bit cleaner function handleOnChange() { // find the selected size option and get its value // size will contain "1", "4", or "16" var size = $prodSize.children(':checked').val() // find the selected bundle option and get its value // bundle will contain "single", "double", or "triple" var bundle = $prodBundle.children(':checked').val() // create a value variable to grab the desired color // to insert into the "cart button" input. var value = '' // make comparisons, similar to what you were doing // except now we are using variables if (size == 1){ if (bundle === 'single') value = 'red'; else if (bundle === 'double') value = 'green'; else if (bundle === 'triple') value = 'blue'; } else if (size == 4){ if (bundle === 'single') value = 'black'; else if (bundle === 'double') value = 'white'; else if (bundle === 'triple') value = 'gray'; } else if (size == 16){ if (bundle === 'single') value = 'orange'; else if (bundle === 'double') value = 'purple'; else if (bundle === 'triple') value = 'pink'; } // update the cart button input with whatever color was picked $cartButton.val(value) } // attach the on change events $prodBundle.on('change', handleOnChange) $prodSize.on('change', handleOnChange) // trigger it for the first time $prodSize.trigger('change') 
 <form id="prod_size"> <input type="radio" name="size" value="1">small</input> <input type="radio" name="size" value="4" checked="checked">medium</input> <input type="radio" name="size" value="16">large</input> </form> <form id="prod_bundle"> <input type="radio" name="prod_bundle" value="single">one</input> <input type="radio" name="prod_bundle" value="double">two</input> <input type="radio" name="prod_bundle" value="triple" checked="checked">three</input> </form> <form> <input id="cart_button_value" value="green"> </form> <script src="https://code.jquery.com/jquery-3.0.0.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script> 

I hope that helps. 我希望有所帮助。

I got another one: Use data attributes to set the values that you want to show on the input, and then ask for them.. 我得到了另一个:使用数据属性来设置要在输入上显示的值,然后请求它们。

<form id="prod_size">
<input type="radio" name="size" value="one">small</input>
<input type="radio" name="size" value="four" checked="checked">medium</input>
<input type="radio" name="size" value="sixteen">large</input>
</form>
<form id="prod_bundle">
<input type="radio" name="prod_bundle" value="single" data-one="red" data-four="black" data-sixteen="orange">single</input>
<input type="radio" name="prod_bundle" value="double" data-one="green" data-four="white" data-sixteen="purple">double</input>
<input type="radio" name="prod_bundle" value="triple" data-one="blue" data-four="gray" data-sixteen="pink" checked="checked">triple</input>
</form>
<form>
<input input id="cart_button_value" value="green"/>
</form>    

And the javascript function should be like this... 而javascript函数应该是这样的......

$(function(){
$('[name="size"]').change(changeInputVal);
$('[name="prod_bundle"]').change(changeInputVal);

function changeInputVal(){
    var sizeSelected = $('input[name="size"]:checked', 'form#prod_size').val();
    var valueToInput = $('input[name="prod_bundle"]:checked', 'form#prod_bundle').data(sizeSelected);
    $("#cart_button_value").val(valueToInput);
  }
});

I hope this could help you 我希望这可以帮到你

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

相关问题 如何使用 JavaScript 更改段落文本,具体取决于表单中选中的单选按钮? - How do I change paragraph text using JavaScript, depending on which radio button is checked in a form? 如何让 jQuery 根据多个单选按钮选择更改 CSS? - How do I get jQuery to change CSS based on multiple radio button selections? 如何使用JQuery检查输入单选按钮? - How do I make an input radio button checked…using JQuery? 如何为包含多个使用单选输入类型的问题的表单编写 html? - How do I write html for form with multiple questions that use radio input type? 如何从两个不同的表单集中选择一个单选按钮? - How do I select only one radio button from two different form sets? JavaScript / jQuery-识别表单输入的类型(即单选按钮,文本输入,下拉菜单) - JavaScript / jQuery - Recognize Type of Form Input (i.e. radio button, text input, dropdown) 使用jQuery,我如何检测到哪个 - Using jQuery, how do I detect which <input type='image> was clicked in a form with multiple submit images? 如何在不重新提交表单的情况下检测单选按钮的值更改? - How to detect the radio button's value change without resubmit the form? jQuery:如何检测由其他事件引起的单选按钮更改 - jQuery: How to detect radio button change caused by other event jQuery / Java单选按钮更改 - Jquery/Javascript radio button on change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM