如何使用jquery更改输入的属性值

时间:2016-05-19 13:35:04

标签: jquery attr

我有以下html结构,并希望在填充另一个输入时更改price属性的值。

<dd>
    <div class="input-box additional-images imprint-images" style="height: 400px; padding-bottom: 0px; outline: medium none; overflow: hidden; width: 889px; padding-right: 0px;" tabindex="0">
        <ul id="options-788-list" class="options-list">
            <li>
                <input type="checkbox" price="0" value="9697" id="options_788_2" name="options[788][]" onclick="opConfig.reloadPrice()" class="checkbox  product-custom-option">
            </li>
        </ul>
    </div>
</dd>
    <div class="color-quantity not-selected-inputs">
        <input type="text" name="custom" onkeydown="return myFunction(event);">
    </div>

我尝试使用以下脚本更改price属性但不起作用:

var quantitys = 0;
jQuery(".color-quantity > input").each(function() {
    if (this.value) {
        quantitys += (this.value) * 1;
    }

    if (parseInt(quantitys) >= "25") {
        alert("more than 25");
        jQuery("dd > .imprint-images  > ul > li > input").attr("price", "5.00");
    }
});

请在这种情况下帮助我。

2 个答案:

答案 0 :(得分:0)

<强> HTML

<dd>
    <div class="input-box additional-images imprint-images" style="height: 400px; padding-bottom: 0px; outline: medium none; overflow: hidden; width: 889px; padding-right: 0px;" tabindex="0">
        <ul id="options-788-list" class="options-list">
            <li>
                <input type="checkbox" data-price="0" value="9697" id="options_788_2" name="options[788][]" onclick="opConfig.reloadPrice()" class="checkbox  product-custom-option">
            </li>
        </ul>
    </div>
</dd>
    <div class="color-quantity not-selected-inputs">
        <input type="text" name="custom" onkeydown="return myFunction(event);">
    </div>

<强> JS

var quantitys = 0;
jQuery(".color-quantity > input").each(function() {
    if (this.value) {
        quantitys += parseInt(this.value);
    }

    if (quantitys >= 25) {
        alert("more than 25");
        $("div.imprint-images > ul > li > input").attr("data-price", "5.00");
    }
});

这会对你有用吗?

答案 1 :(得分:0)

检查一下 我在您的复选框上将价格更改为数据价格 添加虚拟课程 update_price_element

将课程 add_price 添加到文字字段

Hpe有帮助。

&#13;
&#13;
var quantitys = 0;
	$('.add_price').blur(function(){
		var value = $(this).val();
		if (value) {
			quantitys += (this.value) * 1;
		}
		if (parseInt(quantitys) >= "25") {
			alert("more than 25");
			$(".update_price_element").attr("data-price", "5.00");
		}
	});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<dd>
    <div class="input-box additional-images imprint-images" style="height: 400px; padding-bottom: 0px; outline: medium none; overflow: hidden; width: 889px; padding-right: 0px;" tabindex="0">
        <ul id="options-788-list" class="options-list">
            <li>
                <input type="checkbox" data-price="0" value="9697" id="options_788_2" name="options[788][]" onclick="opConfig.reloadPrice()" class="checkbox  product-custom-option update_price_element">
            </li>
        </ul>
    </div>
</dd>
<div class="color-quantity not-selected-inputs">
<input type="text" name="custom" class="add_price" onkeydown="" />
</div>
&#13;
&#13;
&#13;