yii2单选按钮

时间:2015-10-24 09:58:29

标签: yii2

在使用yii2动态表单包时,我在yii2单选按钮中遇到一个问题。 yii2没有生成带id属性的单选按钮。由于单选按钮中缺少id属性,yii2动态表单单选按钮值始终设置为1.所以请帮我解决这个问题。

被修改

<?= $form->field($client_allow_acces, "[$i]access_type")->radioList([1 => 'Allow access', 2 => 'Can\'t allow access'], ['uncheckValue' => null, 'id'=>'custom_id_value']) ?>

我正在获取

下面的html输出
<div class="form-group field-clientallowaccess-0-access_type required">
    <label class="control-label" for="custom_id_value">Access Type</label>
    <input type="hidden" name="ClientAllowAccess[0][access_type]" value="">
    <div id="custom_id_value">
        <label><input type="radio" name="ClientAllowAccess[0][access_type]" value="1"> Allow access</label>
        <label><input type="radio" name="ClientAllowAccess[0][access_type]" value="2"> Can't allow access</label>
    </div>
    <div class="help-block"></div>
</div>

我的自定义ID值来自div元素。但是我需要单选按钮本身。

提前致谢。

1 个答案:

答案 0 :(得分:2)

您需要使用以下代码,因为您的代码中已将您的ID分配给int numberToGuess = 29; int counter; boolean isDone; Scanner sc = new Scanner(System.in); public Model() { isDone = false; run(); } public void run() { welcomeMessage(); while (!isDone) { guessNumber(); } } public void welcomeMessage() { System.out.println("Welcome to " + '\n' + "***** GUESS THE NUMBER *****"); } public void guessNumber() { System.out.println("Please enter number and hit 'Enter'" + '\n'); if (sc.hasNextInt()) { int input = sc.nextInt(); counter++; if (input < numberToGuess) { System.out.println('\n' + "Your guess is too low!" + '\n'); } else if (input > numberToGuess) { System.out.println('\n' + "Your guess is too high!" + '\n'); } else { System.out.println("Congratulations, you guessed the number!" + '\n' + "You guessed the number in " + counter + " guess." + '\n'); isDone(); } } else { System.out.println("Invalid input, please enter a number!" + '\n'); sc.next(); } } public void isDone() { System.out.println("Do you wanna play again? Enter y/n"); if (sc.hasNext()) { String input = sc.next(); if (input.equals("y")) { isDone = false; counter = 0; } else if (input.equals("n")) { isDone = true; } else { System.out.println("Invalid input"); isDone(); } } } 。这是div

<div id="custom_id_value">