I am trying after reading manual get slider working. It returns Null. What I am doing wrong? How do I get current value form slider?
$('#slider-fill').slider();
var value = $('#slider-fill').slider("option", "value");
$(document).on('vclick', '#test', function(){
alert(value);
});
<input name="slider" id="slider-fill" value="1" min="1" max="10" step="1" data-highlight="true" type="range">
sdfsdf
Fiddle: http://jsfiddle.net/smatisen/eupjvf2x/
You want to use jQuery .val() -- which gets the value of an input.
alert($('#slider-fill').val());
http://jsfiddle.net/TheFiddler/eupjvf2x/2/
You shouldn't need the value var at all, I don't think.
Related
I have a value for user display and a similar value for storage. How can I modify what I have so that I save the correct data to the model?
Fiddle
HTML
<div>formatted value for user display</div>
<input type="text" data-bind="value: formattedUnitOfCost" id="Model_Bound_ID" />
<div>unformatted value (the one I'd like to save)...this is not model bound</div>
<input type="text" data-bind="value: unitOfCost" />
JavaScript/Knockout
function AppViewModel() {
var self = this;
self.unitOfCost = ko.observable(1.01).extend({
isServerSet: false
});
self.formattedUnitOfCost = ko.computed({
read: function () {
return '$' + self.unitOfCost().toFixed(2);
},
write: function (value) {
// Strip out unwanted characters, parse as float, then write the raw data back to the underlying "price" observable
value = parseFloat(value.replace(/[^\.\d]/g, ""));
self.unitOfCost(isNaN(value) ? 0 : value); // Write to underlying storage
},
owner: self
});
}
ko.applyBindings(new AppViewModel());
The unformatted value is not displayed to the user. Model_Bound_ID is user editable.
You're doing it the wrong way.
Your model bound control (I mean the control that will be posted to your controller, and has the unformatted value) should be created like any other control, for example using Html.HiddenFor or whatever you want in your (Razor?) template. And you must add the data-bind attribute in the template. Remember that low-dash will be converted in medium-dash, so you can add it in the attributes parameter of the Html Helper like this: { data_bind = "value: unitOfcost" }.
Obviously this hidden field will be sent to the controller when posted (direct post, ajax, or whatever).
Now you need to add the visible control, and bind it to another observable. This observable will be a computed observable, which will do this:
on read, it will take the value from unitOfWork, and return it formatted
on write, it will parse the value to convert it to number, and update the unitOfWork observable with the parsed value
In fact you've got nearly all the code, but were implementing it all the way back.
Another way of looking at this answer is to change the html from this:
<input type="text" data-bind="value: formattedUnitOfCost" id="Model_Bound_ID" />
<input type="text" data-bind="value: unitOfCost" />
to this
<input type="text" data-bind="value: formattedUnitOfCost"/>
<input type="text" data-bind="value: unitOfCost" id="Model_Bound_ID" />
And KO does the rest by the power of the observables. Gosh I love KO
I'm trying get the value from a input but it doesn't work
I have:
<input id="inputA" name="inputA" type="number" value="25" />
and:
var pot = $('#inputA').val();
Also, I have tried:
var pot = $('#inputA').text();
But it doesn't work.
I need help please!
Try to console $('#inputA') before you get its value, if it undefined, may be this document will help you http://jquerymobile.com/demos/1.2.1/docs/api/events.html
So I have a slider in my HTML document:
<input type="range" name="testSlider" id="testSlider" min="0" max="100" value="0" />
I want to modify the min and max range at run time, but I can't find any documentation on how to do this.
I apologize if this is obvious, but I'm really struggling with this seemingly obvious task. Thanks!
You can use jQuery's .attr() method.
$('#testSlider').attr('min',MINVALUE);
$('#testSlider').attr('max',MAXVALUE);
And then you refresh the slider
$('#testSlider').slider( "refresh" );
OR
$("#testSlider").slider( "option", { min: MINVALUE, max: MAXVALUE} );
#Html.RadioButton("smth",true)
This row at the page looks like UNCHECKED radioButton. Why?
You should use
HtmlHelper.RadioButton(string name, object value, bool isChecked)
extension method.
First argument is the name of the form field which is input field generated by html helper.
Second argument is the value of input element. If this radio is selected when the postback to server happens, this value is used.
Third argument is what you are looking for. If it is true it makes radio button selected.
For instance,
#Html.RadioButton("Name", "Value" ,true)
would generate an input element which looks like following,
<input checked="checked" id="Name" name="Name" type="radio" value="Value" />
You need to use something of the form
#Html.RadioButton(id,value,checked (bool true/false))
So
#Html.RadioButton("A","B",true)
For example would produce:
<input checked="checked" id="A" name="A" type="radio" value="B" />
The documentation for this is here
In your previous query, you got this answer.
You asked the same query in it's comment section. The problem was that the second line of code was missing one parameter.
Please check below details....
Parameter Details
Razor Syntax
#Html.RadioButton("smth", "smth", true)
#Html.RadioButtonFor( m => m.Prop, true, new { id = "rdBtn" } )
I have view for entering new entity. On that view I have two buttons: one for saving new entity and another button should enter value into textbox on that form but I can't achieve this, the value in the textbox stays same as it was on page load. Does anyone know how to do this?
Thx
There are two possibilities:
Use javascript
Reload the page with additional query string containing the new value
Example with jquery:
<input type="text" name="foo" id="foo" value="old value" />
<input type="button" id="update" value="Update value" />
and the script
$(function() {
$('#update').click(function() {
$('#foo').val('some new value');
});
});