Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
Knockout JS How to Data-Bind to Static Form Element

I am trying to data bind a form element in knockout.js. However I don't want the element to be editable in the form, or even display the element to the user.

I'm able to correctly data-bind the value to an input element. But I would prefer if I could just pull the value from the HTML or a labels text, or a span. So the below works, but I'm not able to change it so it's not an input that the user can change.

<input id="txtProvider" name="txtProvider" type="text" data-bind="value: $root.session().Resource().provider" />

I tried just using a hidden element with a placeholder, but this didn't work.

<input id="txtProvider" name="txtProvider" type="hidden" placeholder="WebEx" data-bind="value: $root.session().Resource().provider" />

Any suggestions?

Thanks in advance for the help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

What you're doing works, or should work, fine:

<form>
  <input type="hidden" data-bind="value: myHiddenVal" />
</form>

function ViewModel() {
  self.myHiddenVal = ko.observable('Foo');
}

ko.applyBindings(new ViewModel());

This will yield:

<input type="hidden" data-bind="value: myHiddenVal" value="Foo">

Example: https://jsfiddle.net/thebluenile/myves32x/

Though I have to say, I of course don't know your exact use case, but "pulling values from HTML elements" is sort of antithetical to how Knockout, or any other data binding framework, works. If the user doesn't have to edit the field you can just keep it out of the UI altogether. The only valid use case for hidden elements I can think of would be if you were submitting the form to a server side script for processing, instead of sending its contents via AJAX.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda