I have two exactly radio button lists in my page, top and bottom. If I select a radio button in the top list, I would like the bottom list make the same change. Is there any way I could achieve this synchronization?
My radio button lists are in a content page.
<asp:RadioButtonList ID="rblVerification1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="V">Verified</asp:ListItem>
<asp:ListItem Selected="True" Value="NV">Not Verified</asp:ListItem>
</asp:RadioButtonList>
Update:
I wrote following javascript function, but it doesn't work. The first alert return 34, a werid number, I am expecting 2. The second alert return undefined. Can anybody tell what I did is wrong?
function rblVerificationClicked(source, destination) {
alert(source.length);
var selectedValue;
for (var x = 0; x < source.length; x++) {
if (source[x].checked) {
selectedValue = source[x].value;
break;
}
}
alert(selectedValue);
for (var x = 0; x < destination.length; x++) {
destination[x].checked = false;
}
for (var x = 0; x < destination.length; x++) {
if (destination[x].value == selectedValue) {
destination[x].checked = true;
break;
}
}
}
In page_load, I added
rblVerification.Attributes.Add("OnClick", string.Format("rblVerificationClicked('{0}', '{1}');", rblVerification.ClientID, rblVerification1.ClientID));
rblVerification1.Attributes.Add("OnClick", string.Format("rblVerificationClicked('{0}', '{1}');", rblVerification1.ClientID, rblVerification.ClientID));
Resolved:
I found what caused the problem.
asp:RadioButtonList render as input type=”radio” HTML elements.
So to get asp:RadioButtonList client id, call getElementsByTagName(“input”) which will return an array of radio buttons.
The easiest way is to do this via JavaScript. I created some example code below:
The html:
<div id="list1">
<input id="item1" type="radio" name="list1"/>Item 1
<input id="item2" type="radio" name="list1"/>Item 2
<input id="item3" type="radio" name="list1"/>Item 3
<div>
<br/>
<div id="list2">
<input id="item1" type="radio" name="list2"/>Item 1
<input id="item2" type="radio" name="list2"/>Item 2
<input id="item3" type="radio" name="list2"/>Item 3
<div>
The JavaScript:
$("input[name=list1]:radio").bind("change", function(e) {
var chk = $("div#list2")
.children("input[id=" + $(this).attr("id") + "]");
chk.attr("checked", true);
});
I also created an JsFiddle for it so you can see it in action.
Related
I have a list of multiple checkboxes in the form. The user can check all the checkboxes but at least one is required. Without selecting at least one, checkbox they should not be able to submit the form. how to make it in contact form 7?
I have added the script which is not working.
Here is my script
$(document).ready(function () {
$('#checkBtn').click(function() {
checked = $("input[type=checkbox]:checked").length;
if(!checked) {
alert("You must check at least one checkbox.");
return false;
}
});
});
In this only write star(*) after checkbox element in the plugin form
[checkbox* checkbox "test 1" "test 2"]
[submit "Send"]
It is perfectly work.
On this set your code
$('#fm_submit').submit(function(e){
e.preventDefault();
var ck_box = $('input[type="checkbox"]:checked').length;
// return in firefox or chrome console
// the number of checkbox checked
console.log(ck_box);
if(ck_box < 1){
alert('Select any one box');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name = "frmTest[]" id="fm_submit">
<input type="checkbox" value="true" checked="true" >
<input type="checkbox" value="true" checked="true" >
<input type="checkbox" >
<input type="checkbox" >
<input type="submit" id="fm_submit" name="fm_submit" value="Submit">
</form>
<div class="container"></div>
Paste the code from above on the bottom of the form instead of the Additional Settings
$('#fm_submit').submit(function(e){
e.preventDefault();
var ck_box = $('input[type="checkbox"]:checked').length;
// return in firefox or chrome console the number of checkbox checked enter code here
console.log(ck_box);
if(ck_box < 1){ alert('Select any one box'); }
});
I have a textbox on my form:
<input type="text" class="input-group-field" id="draftSearchProducts" name="draftSearchProducts" placeholder="SearchProducts" />
In my controller I have the following:
[HttpGet]
public ActionResult SearchResults(string keywords, int queryLimit = 20, int offset = 0)
{
try
{
ProductSearchResults searchResults = new ProductSearchResults();
ComApiData<GetProductsDataConnector> productData = new ComApiData<GetProductsDataConnector>();
var products = productData.Connector.GetProductBySearch(new ProductRequestParameters()
{
Search = keywords,
LTPMerchantId = merchantId,
QueryLimit = queryLimit,
QueryOffset = offset
});
searchResults.ProductDetails = products.ToList();
return PartialView("_SearchResults", searchResults);
}
catch (Exception ex)
{
throw ex;
}
}
And there is a button on the form:
<a id="draftAddProduct" class="hollow button secondary"><i class="fa fa-plus"></i> Add Product</a>
Since I am new to this side of development, I need a little help. I need to wire up the button to take the value typed in the text call the SearchResults controller (located in a file called ProductsController.cs) and populate a modal dialog box with the results. I have a partial razor page:
#model Sauron.Models.Products.ProductSearchResults
#foreach (var product in Model.ProductDetails)
{
<tr>
<td class="imageColumn">
#if (product.Image != null)
{
<div class="ajax-image-load">
<input type="hidden" id="BlockImageID" value="#product.Image.ImageId" />
<input type="hidden" id="BlockImageWidth" value="89" />
<input type="hidden" id="BlockImageHeight" value="89" />
<input type="hidden" id="BlockImageLoaderGif" value="/images/loader-circle-basic.gif" />
</div>
}
</td>
<td>
#product.SKU
<input type="hidden" id="editID" name="editID" value="#product.ProductId" />
</td>
<td>#(product.Description != null ? product.Description.Name : product.ReceiptName)</td>
#*<td>#(product.ColorId != null ? product.)</td> we might want to gather the color object as a property of this search class model*#
<td>
#{
var textColor = "";
if((product.InventorySummary ?? 0) <= 0){
textColor = "red-text";
}
}
<span class="#textColor">#((product.InventorySummary ?? 0).ToString())</span>
</td>
<td>
#if (product.ProductType != null ? product.ProductType.Equals(ProductType.PACK) : false)
{
<span>#(product.PackQty != null ? string.Format("{0} {1}", product.PackQty.Value, product.ProductType.ToString()) : product.ProductType.ToString())</span>
}
else if (product.ProductType != null ? product.ProductType.Equals(ProductType.CASE) : false)
{
<span>#(product.PackQty != null ? string.Format("{0} {1}", product.PackQty.Value, product.ProductType.ToString()) : product.ProductType.ToString())</span>
}
else
{
<span>#(product.ProductType != null ? product.ProductType.ToString() : "")</span>
}
</td>
</tr>
}
that will display the results. I need to display the results of the _SearchResults.cshtml page in a modal box.
I want to call the SearchResults method from the values entered in the text box to display the results.
Anyone's help is appreciated.
The problem was I was calling the controller by the wrong name. Once I got the right name, it started working.
Code:
$('#draftAddProduct').click(function () {
var keyWord = $('#draftProductModal').val();
$('#draftProductModal').load("#Url.Action("SearchResults","Products")?keywords=chair");
});
You can find an answer for this question here:
Bootstrap Modal Dialog - Loading Content from MVC Partial View
Here is some code on my ASP.NET site. I initially wanted to write
if((n%4)==0) {
<div>
}
//code
if((n%4)==0) {
</div>
}
but ASP.NET wouldn't have it. It had a compile error. It appears it ignores } until I close the div. So I ended up with the below. The if statement causes me the same problem. I know I can conditionally have values by writing class="#(cond?"val":"")" but that only works for values I don'
t know how to conditionally have checked in there. Having the same line written 4 times is pretty ridiculous how do I write this properly?
<div>
#for (int i = 1, n = 0; i < 32; i <<= 1, ++n)
{
if ((looking & i) != 0)
{
<input type="checkbox" name="SomeName" value="#i" id="SomeName_#i" checked>
}
else
{
<input type="checkbox" name="SomeName" value="#i" id="SomeName_#i">
}
<label for="SomeName_#i">#TestApp.Controllers.HomeController.enumFriendlyName[n]</label><br />
}
</div>
<div>
#for (int i = 32, n = 4; i < 256; i <<= 1, ++n)
{
if ((looking & i) != 0)
{
<input type="checkbox" name="SomeName" value="#i" id="SomeName_#i" checked>
}
else
{
<input type="checkbox" name="SomeName" value="#i" id="SomeName_#i">
}
<label for="SomeName_#i">#TestApp.Controllers.HomeController.enumFriendlyName[n]</label><br />
}
</div>
I assume you are talking about razor here. I'll start with attributes.. the trick is to use the html helper rather than writing the html by hand.
#Html.Label("Name", "LabelText")
#Html.CheckBox("Name", isChecked)
Also, for the CheckBox and other helpers you can pass and htmlAttributes anon function, if the value you specify for the attribute evals to null, the attribute won't be included in the rendered html.
What are you doing with the divs? Perhaps if you tell us what you want to do we can suggest something. Are you trying to stripe, because you can do that with CSS.
So, rather than the code duplication you could possible use a partial as the content for each div:
<div class="left">
#Html.Partial(_controls, model)
</div>
<div class="right">
#Html.Partial(_controls, model)
</div>
Or something. I may have the syntax wrong, but basically you can pass a model to your partial which can contain the range of items you want.
The model can be like new { start = 1, finish = 32 }
I want to create a small count button but don't know how to make it in JavaScript...
Here's the code :
HTML
<div id="input_div">
<input type="text" size="25" value="0" id="count">
<input type="button" value="-" id="moins">
<input type="button" value="+" id="plus">
</div>
It must increase AND decrease the number in the input[type=text] when click on the -/+ button.
Can someone help me ?
You'd need two things.
Variables - which are the way to store information in JavaScript
Event handlers, which are the way to react to events in JavaScript
First, let's create a script tag, and put a JavaScript count variable in it, we'll put it in the bottom of our body tag:
<script>
var count = 0;
</script>
Now, we want to create a handler, that is something that executes whenever the plus and minus signs are clicked
<script>
var count = 0;
function plus(){
count++;
}
function minus(){
count--;
}
</script>
We've created two functions to call when the buttons are clicked, but we do not update the value in the HTML, or call them yet, let's update the value in the HTML.
We'll do so by document.getElementByID for the element to update and then change its value. Our script tag should look something liks this:
<script>
var count = 0;
var countEl = document.getElementById("count");
function plus(){
count++;
countEl.value = count;
}
function minus(){
count--;
countEl.value = count;
}
</script>
One last thing, we need to tell the elements in the DOM to execute those handlers.
<div id="input_div">
<input type="text" size="25" value="0" id="count">
<input type="button" value="-" id="moins" onclick="minus()">
<input type="button" value="+" id="plus" onclick="plus()">
</div>
We've added them as event handlers to the DOM reacting to a click on the buttons, completing the task.
Now, here are some things we can improve:
We can use addEventListener to avoid polluting our DOM, and create unobtrusive JavaScript.
We can use a more advanced tool like KnockoutJS to handle binding the value we have to the DOM element instead of updating it ourselves.
We can read Eloquent JavaScript and learn more about how the language works!
Good luck, happy JavaScripting, and happy learning :)
DEMO FIDDLE FOR JAVASCRIPT
code html -
<div id="input_div">
<input type="text" size="25" value="0" id="count" />
<input type="button" value="-" id="minus" onClick = "doMinus();" />
<input type="button" value="+" id="plus" onClick = "doPlus();" />
</div>
code javaScript -
function doMinus(){
document.getElementById("count").value = --document.getElementById("count").value;
}
function doPlus(){
document.getElementById("count").value = ++document.getElementById("count").value;
}
jQuery Version
DEMO FIDDLE FOR JQUERY
code html -
<div id="input_div">
<input type="text" size="25" value="0" id="count" />
<input type="button" value="-" id="minus" />
<input type="button" value="+" id="plus" />
</div>
code jQuery -
$('#minus').click(function(){
$("#count").val(parseInt($("#count").val())-1);
});
$('#plus').click(function(){
$("#count").val(parseInt($("#count").val())+1);
});
U can write some script as shown
<script>
function increase(){
var a = 1;
var textBox = document.getElementById("count");
textBox.value = a;
a++;
}
</script>
<body>
<button type="button" onclick="increase()">+</button>
<input type="text" id="text">
</body>
similarly u can do it for - decrease button
in this case, I use input type range that display a slider :
<input type="range" id="myInputRange" value="15" min="0" max="50" step="1" onchange="document.getElementById('output').textContent=value" ><span id="output">15</span>
(instead of input type number that is not supported by IE)
This seems pretty simple.
(function() {
var count = 0;
var minusButton = document.getElementById("moins");
var plusButton = document.getElementById("plus");
var countBox = document.getElementById("count");
minusButton.onclick = function(e) {
countBox.value = --count;
};
plusButton.onclick = function(e) {
countBox.value = ++count;
};
})();
I'm currently doing the following to compensate for boolean's not mapping well to radio buttons. I am stuck binding 1 and 0 to the value (instead of true and false) because of how the fields are read out of the observables. The value of Pref1/Pref2 come as true/false boolean values from the server. The key here is I want to not only data-bind the checked value of the radio button to match the true/false in the object, but I also want the boolean value of true/false to be written back into the GraduationClass object. My compensation code is not only ugly, but not scalable.
<input type="radio" value="1" name="radioGroup" data-bind="checked: Pref1" />Yes
<input type="radio" value="0" name="radioGroup" data-bind="checked: Pref2" />No
Save
function SiteSettingsViewModel() {
var self = this;
this.saveGraduationClass = function(graduationClass) {
// hack until i get a custom radio button binding
if (graduationClass.Pref1() == 1) {
graduationClass.Pref1(true);
} else {
graduationClass.Pref1(false);
}
if (graduationClass.Pref2() == 1) {
graduationClass.Pref2(true);
} else {
graduationClass.Pref2(false);
}
// ...ajax call to save graduationClass to the server
}
function GraduationClass(data) {
var self = this;
ko.mapping.fromJS(data, {}, this);
}
Here is example from knockoutJs website, that demonstrate how to use radio buttons with
"checked" attribute:
<p>Send me spam: <input type="checkbox" data-bind="checked: wantsSpam" /></p>
<div data-bind="visible: wantsSpam">
Preferred flavor of spam:
<div><input type="radio" name="flavorGroup" value="cherry" data-bind="checked: spamFlavor" /> Cherry</div>
<div><input type="radio" name="flavorGroup" value="almond" data-bind="checked: spamFlavor" /> Almond</div>
<div><input type="radio" name="flavorGroup" value="msg" data-bind="checked: spamFlavor" /> Monosodium Glutamate</div>
</div>
<script type="text/javascript">
var viewModel = {
wantsSpam: ko.observable(true),
spamFlavor: ko.observable("almond") // Initially selects only the Almond radio button
};
// ... then later ...
viewModel.spamFlavor("msg"); // Now only Monosodium Glutamate is checked
</script>
But I dont understand why you use two objects - "Pref1" and "Pref2" fro one radiobutton group "radioGroup"? In this case you just could use one object as in an example used "spamFlavor".
So, please, describe more ditaily what you want to bind: one radiobuttons group by one selected value, or something else.
Also you could use computed observables to calculate different values, please see example.