jQuery堆叠单选按钮

时间:2016-05-09 21:54:56

标签: jquery html radio-button

我正在尝试根据单选按钮的输入创建一个更改地址的链接,但我现在的代码忽略了第一个单选按钮。

var mwc1 = $("#mwc1").is(":checked");
var mwc2 = $("#mwc2").is(":checked");
var mwv2 = $("#mwv2").is(":checked");
var mbc2 = $("#mbc2").is(":checked");
var addr = "";
if (mwc1) {
    if (mwc2) {addr = "1";}
    else if (mwv2) {addr = "2";}
    else if (mbc2) {addr = "3";}
}
$("#checkout a").attr('href', addr);

这是整个代码的jsfiddle。谢谢!https://jsfiddle.net/a2Lkgua3/2/

2 个答案:

答案 0 :(得分:1)

在你的JS小提琴中你重复使用变量名

var mwc1 = $("#mwc1").is(":checked");
var mwv1 = $("#mwv1").is(":checked");
var mbc1 = $("#mbc1").is(":checked");
var mwc1 = $("#mwc2").is(":checked");
var mwv1 = $("#mwv2").is(":checked");
var mbc1 = $("#mbc2").is(":checked");

我认为这是一个错字,你真的想要:

var mwc1 = $("#mwc1").is(":checked");
var mwv1 = $("#mwv1").is(":checked");
var mbc1 = $("#mbc1").is(":checked");
var mwc2 = $("#mwc2").is(":checked");
var mwv2 = $("#mwv2").is(":checked");
var mbc2 = $("#mbc2").is(":checked");

答案 1 :(得分:0)

我知道这已得到解答,但请尝试以下操作,它应该可以为您节省大量代码:

a#checkoutbtn{
		background: #ECECEC;
		padding: 20px 37px;
		display: block;
		color:#7f7f7f;
        width: 145px;
		margin:50px auto;
		margin-top:100px;	
        text-align:center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<ul>
      <li>
          <input type="radio" id="mwc1" name="ms1selector" data-val = 1>
          <label for="mwc1">yahoo</label>
      </li>
  </ul>
   <ul>
       <li>
           <input type="radio" id="mwv1" name="ms1selector" data-val = 2>
           <label for="mwv1">apple</label>
       </li>
   </ul>
   <ul>
       <li>
           <input type="radio" id="mbc1" name="ms1selector" data-val =3>
           <label for="mbc1">bing</label>
  </li>
      </ul>
      <br/><br/><br/>
      <ul>
      <li>
          <input type="radio" id="mwc2" name="ms2selector"data-val =4>
          <label for="mwc2">yahoo</label>
      </li>
  </ul>
  
   <ul>
       <li>
           <input type="radio" id="mwv2" name="ms2selector"data-val =5>
           <label for="mwv2">apple</label>
       </li>
   </ul>
   <ul>
       <li>
           <input type="radio" id="mbc2" name="ms2selector"data-val =6>
           <label for="mbc2">bing</label>
  </li>
      </ul>
      

  <div id="checkout">
      <a id="checkoutbtn" href="www.google.com" > Button </a>
  </div>
namespace Xamarin.Forms
{
    public class CustomButton : Button  
    {
        public CustomButton():base()
        {   
        }

        protected override void OnParentSet()
        {
            base.OnParentSet();
        }
    }
}

希望有所帮助