douhe2305 2013-04-05 20:13
浏览 43
已采纳

我有一个与输入金额直接相关的复选框。 如果有输入金额,则必须在发布前检查复选框

I've created a function for a standard checkbox in php, it's called cbox as well as a standard input box called inp.

<tr><td><?php cbox('charge_cc'); ?> Charge CC? Amount $ <?php inp('charge_amt'); ?></td></tr>

If there is an amount entered into the 'charge_amt' inp field the checkbox needs to be checked as well before they post/submit.

If no amount has been entered into the input field than they do not need to check the box and can proceed to fill out the rest of the form and submit. I'm uncertain on how to accomplish this because i think my php functions for cbox and inp are breaking my jquery/javascript.

As of now i've tried a few variations:

<script type="text/javascript">
function validate(){
if (document.getElementById('charge_cc').checked){
          alert("checked") ;
}else{
alert("You didn't check it! Let me check it for you.")
}
}
</script>

No avail, any help would be greatly appreciated.

<td><input type="hidden" name="charge_cc" value="0" class="charge_cc">
    <input type="checkbox" id="charge_cc" name="charge_cc" value="1" class="charge_cc"> Charge CC? Amount $ <input type="text" id="charge_amt" name="charge_amt" value="" size="8" maxlength="6"></td>
  • 写回答

3条回答 默认 最新

  • dqd22496 2013-04-05 21:02
    关注

    If all you need is to check/uncheck the checkbox depending on an entry in the charge_amt text-input, I'd suggest:

    $('#charge_amt').keyup(function(){
        var that = this;
        $('#charge_cc').prop('checked', function(){
            return that.value.length;
        });
    });
    

    JS Fiddle demo.

    If you want to prevent the user un-checking the checkbox once there's a value in the text-input:

    $('#charge_amt').keyup(function () {
        var test = this.value.length;
        $('#charge_cc').prop({
            'checked': test,
                'disabled': test
        });
    });
    

    JS Fiddle demo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景