javascript - 页面加载时选中的复选框

标签 javascript php

我有一个产品项目页面,当使用 JavaScript 加载页面时,应该选择四个权重选项中的第一个。

它工作正常,但现在在对页面设计进行一些更改后似乎无法工作。

    $( document ).ready( function () {
    			var licnt = $( '#product-nav' ).length;
    			for ( var n = 1; n <= licnt; n++ ) {
    				$( '#price_' + n ).prop( 'checked', true );
    				var pric = document.getElementById( "price_" + n ).value;
    				document.getElementById( "prices_" + n ).innerHTML = "<?php echo $currency; ?>" + pric;
    				$( '.weightval_' + n ).val( "0.50" );
    			}
    		} );
    
    		function outputValue( item ) {
    			var pid = item.getAttribute( "id" );
    			var splitid = pid.split( "_" );
    			document.getElementById( "prices_" + splitid[ 1 ] ).innerHTML = "<?php echo $currency; ?>" + item.value;
    			var wght = item.getAttribute( "wght" );
    			$( '.weightval_' + splitid[ 1 ] ).val( wght );
    		}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form method="post" action="cart_update.php">
    	<div id="product-nav">
    	<div class="child">
    		<div id="prices_$i" class="price"></div>
    	<div id="weight-select" class="select">
    	<fieldset style="border: 0;">
    	<label>
          <input type="radio" name="price" value="<?php echo $obj->price_1 ?>" id="price_$i" wght="<?php echo $obj->weight_1 ?>" wghtid="weight_1" onChange="outputValue(this)" >
          <div>500g</div></label>
          <input type="hidden" name="weight" value="" id="weight_$i" class="weightval_$i">
        <label>
          <input type="radio" name="price" value="<?php echo $obj->price_2 ?>" id="price_$i" wght="<?php echo $obj->weight_2 ?>" wghtid="weight_2" onChange="outputValue(this)">
          <div>1kg</div></label>
          <input type="hidden" name="weight" value="" id="weight_$i" class="weightval_$i" >
        <label>
          <input type="radio" name="price" value="<?php echo $obj->price_3 ?>" id="price_$i" wght="<?php echo $obj->weight_3 ?>" wghtid="weight_3" onChange="outputValue(this)">
          <div>5kg</div></label>
          <input type="hidden" name="weight" value="" id="weight_$i" class="weightval_$i" >
        <label>
          <input type="radio" name="price" value="<?php echo $obj->price_4 ?>" id="price_$i" wght="<?php echo $obj->weight_4 ?>" wghtid="weight_4" onChange="outputValue(this)">
          <div>10kg</div></label>
          <input type="hidden" name="weight" value="" id="weight_$i" class="weightval_$i">
    	<label>
    		<input type="hidden" name="<?php echo $obj->product_code ?>" value="" />
    	</label>
    	</fieldset>
    	<input type="hidden" name="product_code" value="<?php echo $obj->product_code ?>" />
    	<input type="hidden" name="type" value="add" />
    	<input type="hidden" name="return_url" value="<?php echo $current_url ?>" />
    	</div>
    	<div class="qty">
    		<input type="button" value="–" class="qtyminus" field="product_qty" />
        <input type="text" name="product_qty" value="1" class="qty-textbox" style="margin-bottom: 0px !important"/>
        <input type="button" value="+" class="qtyplus" field="product_qty" />
    		</div>
    		<div class="add">
    			<input type="submit" value="ADD" class="add-cart">
    		</div>
    		</div>
    		</div>

最佳答案

您收到错误

Uncaught TypeError: Cannot read property 'value' of null

来自 js 中的以下行

var pric = document.getElementById("price_" + n).value;

这是因为您没有正确解析输入id属性,您使用的变量$i就像

id="price_$i"

其实应该是

id="price_<?=$i?>"

您的输入应如下所示

<input type="radio" name="price" value="<?php echo $obj->price_1 ?>" id="price_<?=$i?>" wght="<?php echo $obj->weight_1 ?>" wghtid="weight_1" onChange="outputValue(this)" >

您可能需要查看此 $i 来自何处,它会起作用

此外,您也有同样的错误,输入具有

<input type="hidden" name="weight" value="" id="weight_$i" class="weightval_$i">

他们应该像

<input type="hidden" name="weight" value="" id="weight_<?=$i?>" class="weightval_<?=$i?>">

关于javascript - 页面加载时选中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49887694/

相关文章:

javascript - 如何跨域请求二进制数据?

javascript - Bootstrap 4 - 移动网站太宽。横向滚动发生。可能的语法错误?

javascript - 如何使 jQuery 插件中的设置(或选项)可用于所有方法?

php - 从数据库获取数字,然后减去它不起作用

php - Magento - 重建索引过程存在问题 - 目录产品

javascript - 在 wordpress 中显示/隐藏子菜单

javascript - 如何在带有 scaleTime 的条形图中使用 x 和宽度?

php - 无法在 PHP 脚本中运行 Linux "awk"命令

php - 从 PHP 的 JSON 中去除前导 'u'

php - 需要将带有方括号的字段名称转换为javascript对象