无法使用jquery将bollean值设置为data属性

时间:2017-06-26 09:35:06

标签: javascript jquery json custom-data-attribute

我有一个带有数据属性的按钮:

<button id="manageEditContract" type="button" class="btn btn-default" data-is-allow-to-edit="@(Model.Contract.IsAllowToEdit)">
       @(Model.Contract.IsAllowToEdit ? "Block" : "Unblock") 
</button>

当用户点击我想要按钮更改其属性时。因为我的action方法返回带有两个属性的JSON对象&#34; IsOk&#34;和#34; IsAllowToEdit&#34;,我写了这个函数:

$('#manageEditContract').on('click', function () {
    var isAllowToEdit = $(this).data('isAllowToEdit');

    $.ajax({
        url: '@Url.Action("ChangeEditOption")',
        type: 'POST',
        data: { isAllowToEdit: isAllowToEdit },
        success: function (data) {
            if (data.IsOk) {
                $('#manageEditContract').data('isAllowToEdit', JSON.stringify(data.IsAllowToEdit));
            }
        }
    });
});

但它没有改变。

我尝试使用JSON.stringify但没有运气。

已更新: 正如答案所示,我应该使用&#34; attr&#34;将值设置为特定属性。但我也意识到我不应该使用&#34;数据&#34;当我得到数据属性的值时,因为它始终是相同的。看起来像&#34;数据&#34;属性缓存数据。 所以,而不是

var isAllowToEdit = $(this).data('isAllowToEdit');

我应该使用:

var isAllowToEdit = $(this).attr('data-isAllowToEdit');

在我的案例中也没有必要使用JSON.stringify。

2 个答案:

答案 0 :(得分:1)

class Msg
  case class Msg1(string: String) extends Msg
  case class Msg2(string: String) extends Msg
  case class Msg3(string: String) extends Msg

def optionFilter[T]: Flow[Option[T], T, NotUsed] = {
  import akka.stream.scaladsl.GraphDSL.Implicits._

val graph = GraphDSL.create() { implicit builder =>
  {
    def partitioner(o: Option[T]) = o.map(_ => 0).getOrElse(1)
    val partition: UniformFanOutShape[Option[T], Option[T]] =
      builder.add(Partition[Option[T]](2, partitioner))

    val flow = builder.add(Merge[T](1))

    partition.out(0) ~> Flow[Option[T]].collect { case Some(t) ⇒ t } ~> flow
    partition.out(1) ~> Flow[Option[T]].collect { case None    ⇒ None } ~> Sink.ignore

    FlowShape(partition.in, flow.out)
    }
  }
  Flow.fromGraph(graph)
  }

val msg1Filter = optionFilter[Msg1]

def router(
  source1: Source[Msg, ActorRef],
  source2: Source[Msg, _],
  sink1: Sink[Msg1, _],
  sink2: Sink[Msg2, _],
  flow: Flow[Msg3, Msg, _]
  ): ActorRef = {
  import akka.stream.scaladsl.GraphDSL.Implicits._

  val graph: Graph[SinkShape[Msg], NotUsed] = GraphDSL.create() { implicit builder =>
    val unzipper: UnzipWith3[Msg, Option[Msg1], Option[Msg2], Option[Msg3]] =
    UnzipWith { msg: Msg =>
      msg match {
        case msg1: Msg1 => (Some(msg1), None, None)
        case msg2: Msg2 => (None, Some(msg2), None)
        case msg3: Msg3 => (None, None, Some(msg3))
      }
    }

  val merge   = builder.add(Merge[Msg](3))
  val forward = builder.add(Merge[Msg](1))

  val unzip = builder.add(unzipper)

  source2 ~> merge ~> unzip.in
  forward.out ~> merge

  unzip.out0 ~> optionFilter[Msg1] ~> sink1
  unzip.out1 ~> optionFilter[Msg2] ~> sink2
  unzip.out2 ~> optionFilter[Msg3] ~> flow ~> merge

  SinkShape(forward.in(0))
}

val sink: Sink[Msg, NotUsed] = Sink.fromGraph(graph)

sink.runWith(source1)
}

val source1 = Source.actorRef(4096, OverflowStrategy.fail)
val source2 = Source(List(Msg2("from source2")))

val sink1: Sink[Msg1, Future[Done]] = Sink.foreach((msg: Msg1) => println(s"sink1: $msg"))
val sink2: Sink[Msg2, Future[Done]] = Sink.foreach((msg: Msg2) => println(s"sink2: $msg"))

val flow = Flow.fromFunction((msg: Msg3) => {
  val msg2 = Msg2("from the flow")
  println(s"flow: forward msg $msg to $msg2")
  msg2
})

val actor = router(source1, source2, sink1, sink2, flow)

actor ! Msg1("from source1 (actor)")
actor ! Msg2("from source1 (actor)")
actor ! Msg3("from source1 (actor)")

那应该有用

答案 1 :(得分:1)

您可以使用.attr。这样您就不需要使用JSON.stringify

var boolValue = true;
$('#manageEditContract').attr('data-is-allow-to-edit', boolValue );

了解它的工作原理。

&#13;
&#13;
var boolValue = true;
$('#manageEditContract').attr('data-is-allow-to-edit', boolValue)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div id="manageEditContract" data-is-allow-to-edit="false">inspect me</div>
&#13;
&#13;
&#13;