I have dynamic check box in my view. But its not working fine its always getting checked
here is my code:
<input type="checkbox" onclick = "InActiveUser('#item.UserId')" id = "#item.UserId" checked="#(item.IsActive == false ? false : true)" />
Please help thanks.
Checked attribute doesn't take true or false as a value. The sole presence of this attribute makes it checked. You have to remove this attribute if you want element to remain unchecked.
Read more in this answer
You have to do it as follows:
<input type="checkbox" onclick = "InActiveUser('#item.UserId')"
id = "#item.UserId" checked="#item.IsActive" />
As Patryk said - you need to change the way the checked attribute is added. Something like this:
<input type="checkbox" onclick = "InActiveUser('#item.UserId')" id = "#item.UserId" #(item.IsActive ? "checked" : "") />
Try that.
Try something like this:
#{
if (ViewBag.IsActive)
{
<input type="checkbox" id="#item.UserId" checked onclick = "InActiveUser('#item.UserId')"/>
}
else
{
<input type="checkbox" id="#item.UserId" onclick = "InActiveUser('#item.UserId')"/>
}
}
Approach - 1
#if(item.IsActive)
{
<input type="checkbox" onclick = "InActiveUser('#item.UserId')"
id = "#item.UserId" checked="checked" />
}
else
{
<input type="checkbox" onclick = "InActiveUser('#item.UserId')"
id = "#item.UserId"/>
}
Approach - 2
You can use below code.
$("input[type='checkbox']").prop('checked', true); // Checked
$("input[type='checkbox']").prop('checked', false); // Un-Checked
If you pay attention to the above code, prop function is used to set the attribute value. You can use JQuery - 1.6 version. Now based upon your condition, you can write Razor code in the JavaScript section like below..
<script type="text/javascript">
$(document).ready(function () { //Some Razor code in JQuery
var status = "#status" == "True" ? true : false;
$("input[type='checkbox']").prop('checked', status);
});
</script>
Server side sample code in View.cshtml
#{
var status = false;
}
Approach - 3
JQuery
<script type="text/javascript">
$(document).ready(function () { //Some Razor code in JQuery
var status = "#status" == "True" ? true : false;
if (!status)
$("input[type='checkbox']").removeAttr('checked');
else
$("input[type='checkbox']").attr('checked', true);
});
</script>
Server side code in cshtml
#{
var status = true;
}
If you pay attention to the above JQuery code, I am removing the checked attribute using the removeAttr in case status value is false and setting it to true using the attr function when status is true.
Related
I have a following situation.
I have a variable bOk into a HTML page. I'd like to update this variable every time a checkbox.checked property was changed.
I think the best way should be using jscript but I don't know how to start this.
That is the reason I did not put a code in this question.
Is there a way to somebody to help me?
Using the onclick event, you can get the state of the checkbox with jQuery whenever it is clicked.
document.getElementById("checkbox-id").onclick = function() {
var bOk = $("#checkbox-id").is(":checked")
}
document.getElementById("checkbox-id").onclick = function() {
var bOk = $("#checkbox-id").is(":checked")
alert(bOk);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="checkbox-id"/>
<input id="chkok" type="checkbox" />Tick
#{var BOK = "Hi..";}
<input type="hidden" value=#BOK id="hdnbok">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#chkok').click(function ()
{
alert($("#hdnbok").val());
})
});
In my project I have created a few checkboxes in a for loop:
for(int i=0; i<10; i++)
{
{<input type="checkbox" name="check"/> #i<br>}
}
I also have a button called "Delete" which is supposed to delete the selected items:
<input type="button" id="DelButton" disabled value="Delete" />
For now the button will always be disabled, but I would like to know how it can become enabled if one or more checkboxes become checked.
Though you don't explicitly mention the use of jQuery, it does come with ASP.NET MVC by default. So here goes...
First, respond to the change event for the checkboxes:
$('input[type="checkbox"]').change(function() {
// respond to change
});
This would be invoked any time any checkbox on the page changes its state. Then within this event handler all you want to know is if any checkbox is checked, right? For that you can just select all checked ones and see if there are any. Something like this:
if ($('input[type="checkbox"]:checked').length > 0) {
// at least one is checked
}
Then in your if/else structure you just want to enable/disable the button. That would look something like this:
$('#DelButton').prop('disabled', false);
Putting it all together, you might have something like this:
$('input[type="checkbox"]').change(function() {
if ($('input[type="checkbox"]:checked').length > 0) {
$('#DelButton').prop('disabled', false);
} else {
$('#DelButton').prop('disabled', true);
}
});
Example
You can do that of JQuery code like this:
$('.checkbox_class').change(function() {
if ($('.checkbox_class:checked').length > 0) {
$('#DelButton').prop('disabled', false);
} else {
$('#DelButton').prop('disabled', true);
}
});
Html code :
<input type="checkbox" class="checkbox_class" />
<input type="checkbox" class="checkbox_class" />
<input type="button" id="DelButton" disabled value="Delete" />
I hope to help you.
Live Demo here : http://jsfiddle.net/CdwpA/1656/
You have to initiate an event
Something like :
<input type="submit" name="DelButton" class="inputButton" id="DelButton" value=" DelButton" />
<input type="checkbox" onchange="document.getElementById('DelButton').disabled = !this.checked;" />
I have a K-button on kendo grid, when onclick of that button I am expecting to redirect to controller with parameters, but onclick of that button it is not doing anything, even I kept debugger but still no go, it is not at all triggering am I missing anything let me know, I have tried both " and ' along with different format like $(".info").on('click', function ()
CODE
<script id="template" type="text/kendo-template">
#if(ResultFormatID != 3) { #
<input type="checkbox" #= data.Action ? checked="checked" : "" # class=\"check_row\"/>
# } else { #
<input type="button" class="k-button info" name="info" value="Preview" />
# } #
</script>
$('.info').click(function() {
debugger;
var row = $(this).closest("tr");
var item = Grid.dataItem(row);
window.location.href = '/Login/FreeText?profileID=' + rowIDs + '&billid=' + billID;
});
try this code
<a class="info k-button" >Preview</a>
$(document).one('click', '.info', function () {
console.log("clicked");
});
sorry if the title isn't clear, i was trying to explain in the best possible way. The situation I have is the following: I have a newsletter with a submit button. I have used jquery to unlock the SUBMIT button after the correct email is typed in. It is working fine apart of one thing - I have to click anywhere outside the field (not on the submit button) to enable the submit, then on submit. There was something I have to add somewhere around this line i think, but I dont know what:
document.getElementById("submit").disabled = false;
the full code is here:
HTML:
<div class="element">
<input type="text" id="email" name="email" value="Enter your email" />
</div>
<div id="bsubmit">
<input name="submit" id="submit" src="_images/generic/send_button.png?submit+button=" type="image" disabled="disabled" />
</div>
and jq:
$.fn.swapText = function(){
return this.each(function(){
var tmpDefVal = $(this).val();
$(this).css('color', '#999');
$(this).focus(function(){
if($(this).val() == tmpDefVal){
$(this)
.css('color', '#000')
.val('');
}
});
$(this).blur(function(){
if($(this).val() == ''){
$(this)
.css('color', '#999')
.val(tmpDefVal);
}
});
});
}
$(document).ready(function(){
$('#email').swapText();
$('#email').change(function(){
var regexp = /^\w+[#]\w+\.\w{2}$/;
if(regexp.test($(this).val())){
$(this).removeClass('err');
$(this).addClass('ok');
document.getElementById("submit").disabled = false;
}else{
$(this).removeClass('ok');
$(this).addClass('err');
}
});
});
Thanks for any help.
One problem is that you have two elements with an id of submit.
I'm exploring the potential of jquery to satisfy some of our UI requirements, and am experiencing some curious behaviour. I'm very new to jQuery, and I'm trying to implement a basic pub-sub type of pattern that's hooked into the show & hide functions.
Despite the custom event mechanism looking perfectly simple on the surface, it isn't behaving as I expect. I can't see my syntactical mistake, so I must be misunderstanding the way these custom events are intended to work.
When I execute this code, Here's what I think should happen.
Initially (after doc.Ready) the question2 element should be hidden.
When I click on the 'Vermont' radio button, question 2 should be made visible followed by one alert box indicating that 'question2 has been made visible'.
When I click on another radio button, question 2 should be hidden followed by one alert box indicating that question 2 has been made hidden.
What is actually happening is that I get numerous alert boxes when making question 2 visible, and none when I hide it?
Please help me understand why it's doing this.
Here is the script:
<script type="text/javascript">
function processRadioButtonASD() {
var isChecked = ($("input[name=question1]:checked").val() == "question1.Vermont");
if (isChecked == true) {
$("[data-uniquename=question2]").show(250);
} else {
$("[data-uniquename=question2]").hide(250);
}
}
function detectVisibilityChange(uniqueName) {
$("[data-uniquename=" + uniqueName + "]").bind("madeVisible", function () {
alert($(this).attr("data-uniquename") + " was made visible");
});
$("[data-uniquename=" + uniqueName + "]").bind("madeHidden", function () {
alert($(this).attr("data-uniquename") + " was made hidden");
});
}
$(function () {
$.each(["show", "hide"], function () {
var _oldFn = $.fn[this];
$.fn[this] = function () {
var wasVisible = $(this).is(':visible');
var result = _oldFn.apply(this, arguments);
var isVisible = $(this).is(':visible');
if ((isVisible == true) && (wasVisible == false)) {
$(this).triggerHandler("madeVisible");
} else if ((isVisible == false) && (wasVisible == true)) {
$(this).triggerHandler("madeHidden");
}
return result;
}
});
});
$(document).ready(function () {
processRadioButtonASD();
detectVisibilityChange("question2");
$("input[name='question1']").change(function () { processRadioButtonASD(); });
});
</script>
Here is the html:
<div id="content">
<div id="radioButtonASD" class="example">
<h2>radio button visibility trigger</h2>
<div data-uniquename="question1" class="question">
<label for="question1">
Question 1) (select Vermont to show Question2)
</label>
<br />
<label data-uniquename="question1.Maine">
<input name="question1" data-uniquename="question1.Maine" type="radio" value="me" />Maine</label><br />
<label data-uniquename="question1.Vermont">
<input name="question1" data-uniquename="question1.Vermont" type="radio" value="question1.Vermont" />Vermont</label><br />
<label data-uniquename="question1.NewHampshire">
<input name="question1" data-uniquename="question1.NewHampshire" type="radio" value="question1.NewHampshire" />New
Hampshire</label><br />
<label data-uniquename="question1.Conneticut">
<input name="question1" data-uniquename="question1.Conneticut" type="radio" value="question1.Conneticut" />Conneticut</label><br />
<label data-uniquename="question1.Massachusetts">
<input name="question1" data-uniquename="question1.Massachusetts" type="radio" value="question1.Massachusetts" />Massachusetts
</label>
</div>
<br />
<div data-uniquename="question2" class="question">
<label>
Question 2)
</label>
<br />
<select>
<option data-uniquename="question2.honda" value="honda">Honda</option>
<option data-uniquename="question2.volvo" value="volvo">Volvo</option>
<option data-uniquename="question2.saab" value="saab">Saab</option>
<option data-uniquename="question2.mercedes" value="mercedes">Mercedes</option>
<option data-uniquename="question2.audi" value="audi">Audi</option>
</select>
</div>
</div>
</div>
Thanks for looking.
I came up with an alternative way.
$.each(["show", "hide"], function() {
var effect = $.fn[this];
$.fn[this] = function(duration, move, callback) {
// Match the arguments
var speed = duration;
var easing = callback && move || move && !jQuery.isFunction( move ) && move;
var fn = callback || !callback && move || jQuery.isFunction( duration ) && duration;
// Wrap the callback function
var wrapped = fn;
var wasVisible = $(this).is(':visible');
fn = function(){
var isVisible = $(this).is(':visible');
$.proxy(wrapped, this);
if ((isVisible == true) && (wasVisible == false)) {
$(this).triggerHandler("madeVisible");
} else if ((isVisible == false) && (wasVisible == true)) {
$(this).triggerHandler("madeHidden");
}
};
// Run the effect with the wrapped callback
return effect.call(this, speed, easing, fn);
};
});
The idea is make use of the callback function. From there you can refactor and clean the code.
Take a look at a working example.