form在提交前改变某个属性或者值

网上说了一大堆,但是都是使用插件,jquery.form.js这个吧,但是我这里不需要,简单的写个function就行了.
直接上代码:

<form class="form-horizontal" id="myForm" action="{{url('channelForm/store_channel_form')}}" method="post" enctype="multipart/form-data" role="form">
    <div class="form-group">
        <label for="g_nameuard" class="col-xs-4 col-sm-2 control-label"><span style="color:red;">* span>微信:label>
        <div class="col-xs-8 col-sm-9">
            <input type="radio" name="wechat" value="2" id="wechat_display_required" checked>
            <label for="wechat_display_required" class="margin_right">显示-必填label>
            <input type="radio" name="wechat" value="1" id="wechat_display_nullable">
            <label for="wechat_display_nullable" class="margin_right">显示-非必填label>
            <input type="radio" name="wechat" value="0" id="wechat_none">
            <label for="wechat_none" class="margin_right">不显示label>
        div>
    div>
    <div class="form-group">
        <label for="g_nameuard" class="col-xs-4 col-sm-2 control-label"><span style="color:red;">* span>手机:label>
        <div class="col-xs-8 col-sm-9">
            <input type="radio" name="telephone" value="2" id="telephone_display_required" checked>
            <label for="telephone_display_required" class="margin_right">显示-必填label>
            <input type="radio" name="telephone" value="1" id="telephone_display_nullable">
            <label for="telephone_display_nullable" class="margin_right">显示-非必填label>
            <input type="radio" name="telephone" value="0" id="telephone_none">
            <label for="telephone_none" class="margin_right">不显示label>
        div>
    div>
    <div class="form-group">
        <label for="g_nameuard" class="col-xs-4 col-sm-2 control-label"><span style="color:red;">* span>年龄:label>
        <div class="col-xs-8 col-sm-9">
            <input type="radio" name="age" value="2" id="age_display_required" checked>
            <label for="age_display_required" class="margin_right">显示-必填label>
            <input type="radio" name="age" value="1" id="age_display_nullable">
            <label for="age_display_nullable" class="margin_right">显示-非必填label>
            <input type="radio" name="age" value="0" id="age_none">
            <label for="age_none" class="margin_right">不显示label>
        div>
    div>
    <div class="form-group">
        <label for="g_nameuard" class="col-xs-4 col-sm-2 control-label"><span style="color:red;">* span>邮箱:label>
        <div class="col-xs-8 col-sm-9">
            <input type="radio" name="email" value="2" id="email_display_required" checked>
            <label for="email_display_required" class="margin_right">显示-必填label>
            <input type="radio" name="email" value="1" id="email_display_nullable">
            <label for="email_display_nullable" class="margin_right">显示-非必填label>
            <input type="radio" name="email" value="0" id="email_none">
            <label for="email_none" class="margin_right">不显示label>
        div>
    div>
    <input id="radios" type="hidden" name="radios">#我要在提交之前改变这个值
    
    <div class="form-group">
        <div class="col-sm-offset-2 col-xs-8 col-sm-9">
            <input type="submit" class="btn btn-lg btn-primary" onclick="changeRadios()" value="提交">
            <input type="reset" class="btn btn-lg btn-danger" style="margin-left: 20px;" value="重置">
        div>
    div>
form>

我需要在提交表单之前设置一个input的值,就这么简单就行了,onclik的时候会先执行定义的方法,然后再提交.

<script type="text/javascript">
    function changeRadios() {
        var radios = $("input[type='radio'][name='wechat']:checked").val()+
            $("input[type='radio'][name='telephone']:checked").val() +
            $("input[type='radio'][name='age']:checked").val() +
            $("input[type='radio'][name='email']:checked").val();
        $("#radios").val(radios);
    }
</script>

你可能感兴趣的:(jquery)