复选框默认的checked =“ checked”不起作用?

时间:2018-12-04 22:41:14

标签: javascript jquery html checkbox

复选框默认选中不起作用! 我试图修复它,但是我找不到错误在哪里? 所以在选中的页面加载之后,在未选中的页面加载之后! 我尝试过了

<div class="onoffswitch" style="margin: 0 auto;">
<input type="checkbox" class="avacheckbox onoffswitch-checkbox" id="AvButtonAutoGames" checked="checked"/>
<label class="onoffswitch-label" for="AvButtonAutoGames">
    <span class="onoffswitch-inner"></span>
    <span class="onoffswitch-switch"></span>
</label>
</div>

那个

  <div class="onoffswitch" style="margin: 0 auto;">
    <input type="checkbox" class="avacheckbox onoffswitch-checkbox" id="AvButtonAutoGames" checked/>
    <label class="onoffswitch-label" for="AvButtonAutoGames">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
    </div>

2 个答案:

答案 0 :(得分:3)

要指定值,请使用checked =“ true”,否则请不要指定属性。

Checked: <input type="checkbox" checked="true" /><br/>
Not checked: <input type="checkbox" />

答案 1 :(得分:1)

很难看到,但是您正在使用此语法定义复选框的值吗?

puts "Welcome to 'Get My Number!'"
print "What's your name? "

input = gets

name = input.chomp

puts "Welcome, #{name}!"

# Store a random number for the player to guess.

puts "I've got a random number between 1 and 100."
puts "Can you guess it?"
target = rand(100) + 1

# Track how many guesses the player has made.

num_guesses = 0

# Track whether player has guessed correctly.

guessed_it = false

until num_guesses == 10 || guessed_it

  puts "You've got #{10 - num_guesses} guesses left."
  print "Make a guess: "
  guess = gets.to_i

  num_guesses += 1

# Compare guess to target and print appropriate message.

  if guess < target
    puts "Oops. Your guess was LOW."
  elsif guess > target
    puts "Oops. Your guess was HIGH."
  elsif guess == target
    puts "Good job, #{name}!"
    puts "You guessed my number in #{num_guesses} guesses!"
    guessed_it = true
  end
end

#If the player didn't guess in time, show the target number.

unless guessed_it
  puts "Sorry. You didn't get my number. (It was #{target}.)"
end

如果是这样,这是不正确的-但是在第一个JS示例中,您有正确的document.getElementById('AvButtonAutoGames').checked = true 用法:

prop

$('#AvButtonAutoGames').prop('checked', true) 中一样,要应用的语法只是HTML5或为了清楚起见<input checked>进行扩展。缺少<input checked="checked">属性会导致未选中输入字段。

只需将您的checked转换为函数调用,它应该可以工作